﻿#include "guild_members_logic.h"
#include <vector>
#include <unordered_map>
#include "GuildMember_struct.h"
#include "GuildModuleMembers_struct.h"

#include "GuildAuctionAttentionInfo_struct.h"
#include "GuildRepositoryCellRoleView_struct.h"
#include "GuildRepoCell_struct.h"
#include "GuildAuctionRole_struct.h"
#include "GuildRepositoryAuctionItemRoleView_struct.h"

#include "GuildAuctionRoleInfo_struct.h"
#include "GuildRepositoryAuctionRoleView_struct.h"

#include "GuildAuctionItem_struct.h"

#include "GetGuildRepositoryRoleViewRes_struct.h"

#include "GuildModuleRepository_struct.h"
#include "error/make_error_code.h"
#include "guild/guild_loader.hpp"
#include "guild/module/guild_module_loader.hpp"
#include "guild/module/guild_module_tool.hpp"

namespace guild_service {
bool GuildMembersLogic::GetGuildModuleMembers(const guild_id_type guild_id,
                                   GuildModuleMembers &members) {
  return detail::GetGuildModule<GuildModuleMembers>(kGuildServiceQueryName, guild_id,
                                              kGuildModuleMembersName, members);
}

bool GuildMembersLogic::GetGuildMember(const role_uuid_type role_uuid,
                                       const role_uuid_type member_role_uuid,
                                       GuildMember &member, error_code &ec) {
  if (role_uuid == kInvalidRoleUuid) {
    ec = ErrorCode::ERR_INVALID_ROLE_UUID;
    return false;
  }

  guild_id_type guild_id = kInvalidGuildId;
  if (!detail::LoadGuildIdByRole(kGuildServiceQueryName, role_uuid, guild_id)) {
    ec = ErrorCode::ERR_GUILD_NOT_EXIST;
    return false;
  }

  if (guild_id == kInvalidGuildId) {
    ec = ErrorCode::ERR_INVALID_GUILD_ID;
    return false;
  }

  GuildModuleMembers guild_members;
  if (!GuildMembersLogic::GetGuildModuleMembers(guild_id, guild_members)) {
    ec = ErrorCode::ERR_GUILD_MEMBERS_LOAD_FAILED;
    return false;
  }

  for (const auto &it : guild_members.members) {
    if (it.role_uuid == role_uuid) {
      member = it;
      return true;
    }
  }

  ec = ErrorCode::ERR_GUILD_MEMBERS_ROLE_NOT_EXIST;

  return false;
}

bool GuildMembersLogic::GetGuildModuleMembers(const role_uuid_type role_uuid,
                                        GuildModuleMembers &member_list,
                                        error_code &ec) {
  member_list.members.clear();

  if (role_uuid == kInvalidRoleUuid) {
    ec = ErrorCode::ERR_INVALID_ROLE_UUID;
    return false;
  }

  guild_id_type guild_id = kInvalidGuildId;
  if (!detail::LoadGuildIdByRole(kGuildServiceQueryName, role_uuid, guild_id)) {
    ec = ErrorCode::ERR_GUILD_NOT_EXIST;
    return false;
  }

  if (guild_id == kInvalidGuildId) {
    ec = ErrorCode::ERR_INVALID_GUILD_ID;
    return false;
  }

  if (!GuildMembersLogic::GetGuildModuleMembers(guild_id, member_list)) {
    ec = ErrorCode::ERR_GUILD_MEMBERS_LOAD_FAILED;
    return false;
  }

  return true;
}

bool GuildMembersLogic::GetModuleRepository(
    const guild_id_type guild_id, GuildModuleRepository &module_repository) {
  return detail::GetGuildModule<GuildModuleRepository>(
      kGuildServiceQueryName, guild_id, kGuildModuleRepositoryName,
      module_repository);
}

// TODO members -> welfare
bool GuildMembersLogic::GetGuildRepositoryRoleView(
    const role_uuid_type role_uuid, GetGuildRepositoryRoleViewRes &res,
    error_code &ec) {
  if (role_uuid == kInvalidRoleUuid) {
    ec = ErrorCode::ERR_INVALID_ROLE_UUID;
    return false;
  }

  guild_id_type guild_id = kInvalidGuildId;
  if (!detail::LoadGuildIdByRole(kGuildServiceQueryName, role_uuid, guild_id)) {
    ec = ErrorCode::ERR_GUILD_NOT_EXIST;
    return false;
  }

  GuildModuleRepository module_repository;
  if (!GuildMembersLogic::GetModuleRepository(guild_id, module_repository)) {
    ec = ErrorCode::ERR_GUILD_MOD_REPOSITORY_LOAD_FAILED;
    return false;
  }

  // TODO role_view.cell_capacity = 0;
  // TODO role_view.cell_view
  // TODO role_view.auction_view
  // TODO role_view.next_auction_timestamp = 0;

  return true;
}

bool GuildMembersLogic::QueryGuildWelfareAward(const role_uuid_type role_uuid,
                                               uint64_t &award,
                                               error_code &ec) {
  if (role_uuid == kInvalidRoleUuid) {
    ec = ErrorCode::ERR_INVALID_ROLE_UUID;
    return false;
  }
  guild_id_type guild_id = kInvalidGuildId;
  if (!detail::LoadGuildIdByRole(kGuildServiceWelfareName, role_uuid,
                                 guild_id)) {
    ec = ErrorCode::ERR_GUILD_NOT_EXIST;
    return false;
  }
  award = 0;
  if (!detail::GuildModuleTool::CalMemberWelfareAward(
          kGuildServiceQueryName, guild_id, role_uuid, award)) {
    return false;
  }
  return true;
}

}  // namespace guild_service